import { Button, Drawer, Popover, List, Radio, Tag } from 'antd';
import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import Move from '../Move'
import { CopyOutlined, SyncOutlined, UserOutlined } from '@ant-design/icons'
// jira网站按钮
export function JiraBtns() {

    return (
            <div className='jira-btns'>
                <Button className='aui-button' onClick={(e) => { e.stopPropagation(); copyIssue() }} title="复制问题号和问题描述"><CopyOutlined /></Button>
                <Button className='aui-button' onClick={(e) => { e.stopPropagation(); copyPersonInfo() }} title="复制联系人或邮箱"><UserOutlined /></Button>
                <Button className='aui-button' onClick={(e) => { openQuestionDialog(); }} title="关闭问题">关闭问题</Button>
                {/* <Button onClick={(e) => { toggleJiraDialog() }} title="显示/隐藏 jira修改完成弹窗">显示/隐藏</Button> */}
            </div>
    )
}
export function JiraToolbarBtns() {
    return (
        <Move>
            <div className='jira-right-btns' style={{}}>
                <Button onClick={(e) => { answerSupportQuestion('qq', '') }} title="">qq</Button>
                <Button onClick={(e) => { answerSupportQuestion('qq', '和后端补丁') }} title="">qq+后端</Button>
                <Button onClick={(e) => { answerSupportQuestion('友空间') }} title="">友空间</Button>
                <Button onClick={(e) => { answerSupportQuestion('友空间', '和后端补丁') }} title="">友空间+后端</Button>
            </div>            
        </Move>
    )
}
export function jiraInit () {
    topButtonInit ()
    // if (document.querySelector('#header-details-user-fullname').title.includes('游行行')) {
    //     rightButtonInit ()
    // }         
}
function topButtonInit () {
    // 问题顶部按钮区
    let div = document.createElement('div')
    ReactDOM.render(<JiraBtns></JiraBtns>,div);
    document.body.append(div)
    let timer
    timer = setInterval(() => {
        // 列表和详细视图都有的dom元素
        let issueHeader = document.querySelector('.issue-header-content')
        if (issueHeader) {
            let issueHeaderRect= issueHeader.getBoundingClientRect()
            div.style.display = 'block'
            div.style.position = 'fixed'
            div.style.zIndex = '1111'
            div.style.top = issueHeaderRect.top +'px'
            div.style.left = issueHeaderRect.left + 360 +'px'
        } else {
            div.style.display = 'none'
        }
    }, 1000);
}

function rightButtonInit () {
    // 问题顶部按钮区
    let div = document.createElement('div')
    ReactDOM.render(<JiraToolbarBtns></JiraToolbarBtns>,div);
    document.body.append(div)
}


function openQuestionDialog() {
    let keyVal = document.querySelector('#key-val').innerText
    //  判断是开发问题还是支持问题
    if (keyVal.includes('SUPPORT')) {
        let kaishixiugai = document.querySelector('#action_id_301')
        if (kaishixiugai) {
            kaishixiugai.click() // 开始修改
        } 
        setTimeout(() => {
            //  支持问题修改完成
            let action_id_61 = document.querySelector('#action_id_61')
            if (action_id_61) {
                action_id_61.click()
                setTimeout(() => {
                    // 支持问题填制


                }, 500);                 
            } 
        }, 1500);                  
    } else {
        let kaishixiugai = document.querySelector('#action_id_41')
        if (kaishixiugai) {
            kaishixiugai.click() // 开始修改
        }
        setTimeout(() => {
            //  开发问题修改完成
            let action_id_501 = document.querySelector('#action_id_501')
            if (action_id_501) {
                action_id_501.click()
                setTimeout(() => {
                    document.querySelector('#customfield_11264').value = '已修复' // 已修复
                    document.querySelector('#resolution').value = 10001
                    document.querySelector('#customfield_11263').value = 10653
                    setTimeout(() => {
                        document.querySelector('#customfield_11264').focus()
                        document.querySelector('#customfield_11264').select()
                        // 点击确定
                        // document.querySelector('#issue-workflow-transition-submit').click()
                    }, 100);
                }, 500);            
            }
        }, 1500);
    }

}









// 复制方法
function copyStr(content, message) {
    try {
        
        var url = content; //复制的网址或内容
        var aux = document.createElement('input');
        aux.setAttribute('value', url);
        document.body.appendChild(aux);
        aux.select();
        document.execCommand('Copy'); //原生copy方法执行浏览器复制命令
        document.body.removeChild(aux); //清除input
        if (message == null) {
            /* 复制失败消息 */
        } else {
            /* 复制成功消息 */
        }
    } catch (error) {
        
    }
}
// jiraDialog 显示和隐藏
let jiraDialogFlag = true
function toggleJiraDialog() {
    document.querySelector('.jira-dialog').style.display = jiraDialogFlag ? 'none' : 'block'
    jiraDialogFlag = !jiraDialogFlag
}


export function answerSupportQuestion(type, HouDuan = '') {
    openQuestionDialog()
    setTimeout(() => {
        document.querySelector('#customfield_11210').value = `通过${type}发前端合集补丁${HouDuan}给顾问,打完补丁请用谷歌浏览器【清除缓存】后复测。【注意】通版补丁会覆盖专项改动和二开改动，若有二开和专项，则不能打此通版补丁`
        document.querySelector('#customfield_11216').value = 10511
        document.querySelector('#customfield_11225').value = 10534
        document.querySelector('#customfield_11803').value = 11827
        document.querySelector('#customfield_11263').value = 10653
    }, 1500);
    
}

function copyIssue() {
    let txt = document.querySelector('#key-val').innerText + ' ' + document.querySelector('#summary-val').innerText
    copyStr(txt)
}
function copyPersonInfo() {
    let str = document.querySelector('#project-name-val').innerText
    // 支持问题
    if (str.includes('支持')) {
        let txt1 = document.querySelectorAll('.issue-body-content .item a')[1].innerText
        copyStr(txt1)
    } else {
        // 测试问题
        let txt2 = document.querySelector('#reporter-val').innerText
        copyStr(txt2)
    }
}
// Jira网站弹窗支持拖拽
function jiraDialogDragPlus () {
    var isMouseDown = false;
    // 累加偏移量
    var boxPosition = { x:0,y:0}
    var startPosition = {}
    document.addEventListener('mousedown',(e)=>{
        startPosition.x = e.clientX
        startPosition.y = e.clientY
        if (e.target.classList.contains('jira-dialog-heading') || e.target.parentElement.classList.contains('jira-dialog-heading')) {
            isMouseDown = true
        }
    })
    document.addEventListener('mouseup',(e)=>{
        isMouseDown = false
    })
    document.addEventListener('mousemove',(e)=>{
        if (isMouseDown) {
            // 计算位移量
            boxPosition.x += e.clientX - startPosition.x
            boxPosition.y += e.clientY - startPosition.y
            startPosition.x = e.clientX
            startPosition.y = e.clientY           
            document.querySelector('.jira-dialog').style.transform = `translate(${boxPosition.x}px,${boxPosition.y}px)`
        }
    })
 
}
jiraDialogDragPlus()